home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 August / CHIP_CD_2004-08.iso / software / amc / amc_install.exe / {app} / Scripts / IMDB (pic).ifs < prev    next >
Encoding:
Text File  |  2004-03-20  |  12.0 KB  |  414 lines

  1. // GETINFO SCRIPTING
  2. // IMDB (US) import with small picture
  3.  
  4. (***************************************************
  5.  *  Movie importation script for:                  *
  6.  *      IMDB (US), http://us.imdb.com              *
  7.  *                                                 *
  8.  *  (c) 2002-2004 Antoine Potten                   *
  9.  *                          software@antp.be       *
  10.  *  Contributors :                                 *
  11.  *    Danny Falkov                                 *
  12.  *    Kai Blankenhorn                              *
  13.  *                                                 *
  14.  *  For use with Ant Movie Catalog 3.4.0           *
  15.  *  www.antp.be/software/moviecatalog              *
  16.  *                                                 *
  17.  *  This program is free software; you can         *
  18.  *  redistribute it and/or modify it under the     *
  19.  *  terms of the GNU General Public License as     *
  20.  *  published by the Free Software Foundation;     *
  21.  *  either version 2 of the License, or (at your   *
  22.  *  option) any later version.                     *
  23.  ***************************************************)
  24.  
  25. program IMDb;
  26.  
  27. const
  28.   DescriptionToImport = 2;
  29.    {
  30.       2 = import longest
  31.       1 = import short (from main page, faster)
  32.       0 = display list to select a description
  33.    }
  34.  
  35. var
  36.   MovieName: string;
  37.   MovieURL: string;
  38.  
  39. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  40. var
  41.   i: Integer;
  42. begin
  43.   result := -1;
  44.   if StartAt < 0 then
  45.     StartAt := 0;
  46.   for i := StartAt to List.Count-1 do
  47.     if Pos(Pattern, List.GetString(i)) <> 0 then
  48.     begin
  49.       result := i;
  50.       Break;
  51.     end;
  52. end;
  53.  
  54. procedure AnalyzePage(Address: string);
  55. var
  56.   Page: TStringList;
  57. begin
  58.   Page := TStringList.Create;
  59.   Page.Text := GetPage(Address);
  60.   if pos('<title>IMDb', Page.Text) = 0 then
  61.   begin
  62.     AnalyzeMoviePage(Page)
  63.   end else
  64.   begin
  65.     PickTreeClear;
  66.     AddMoviesTitles(Page, '<b>Exact Matches</b>');
  67.     AddMoviesTitles(Page, '<b>Partial Matches</b>');
  68.     AddMoviesTitles(Page, '<b>Approximate Matches</b>');
  69.     if PickTreeExec(Address) then
  70.       AnalyzePage(Address);
  71.   end;
  72.   Page.Free;
  73. end;
  74.  
  75. procedure AnalyzeMoviePage(Page: TStringList);
  76. var
  77.   Line, Value, Value2, FullValue: string;
  78.   LineNr: Integer;
  79.   BeginPos, EndPos, DescrImport: Integer;
  80. begin
  81.   DescrImport := DescriptionToImport;
  82.   if (DescrImport <> 1) and (Pos('<a href="plotsummary">', Page.Text) = 0) then
  83.     DescrImport := 1;
  84.  
  85.   MovieURL := 'http://imdb.com/title/tt' + copy(Page.Text, pos('<a href="/title/tt',Page.Text)+19, 7);
  86.  
  87.   // URL
  88.   SetField(fieldURL, MovieURL);
  89.  
  90.   // Original Title & Year
  91.   LineNr := FindLine('<title>', Page, 0);
  92.   Line := Page.GetString(LineNr);
  93.   if LineNr > -1 then
  94.   begin
  95.     BeginPos := pos('<title>', Line);
  96.     if BeginPos > 0 then
  97.       BeginPos := BeginPos + 7;
  98.     EndPos := pos('(', Line);
  99.     if EndPos = 0 then
  100.       EndPos := Length(Line);
  101.     Value := copy(Line, BeginPos, EndPos - BeginPos - 1);
  102.     HTMLDecode(Value);
  103.     SetField(fieldOriginalTitle, Value);
  104.     BeginPos := pos('(', Line) + 1;
  105.     if BeginPos > 0 then
  106.     begin
  107.       EndPos := Pos('/I', Line);
  108.       if EndPos < BeginPos then
  109.         EndPos := Pos(')', Line);
  110.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  111.       SetField(fieldYear, Value);
  112.     end;
  113.   end;
  114.  
  115.   // Rating
  116.   LineNr := FindLine('User Rating:', Page, 0);
  117.   if LineNr > -1 then
  118.   begin
  119.     Line := Page.GetString(LineNr + 4);
  120.     if Pos('/10', Line) > 0 then
  121.     begin
  122.       BeginPos := pos('<b>', Line) + 3;
  123.       Value := IntToStr(Round(StrToInt(StrGet(Line, BeginPos), 0) + (StrToInt(StrGet(Line, BeginPos + 2), 0) / 10)));
  124.       SetField(fieldRating, Value);
  125.     end;
  126.   end;
  127.  
  128.   // Picture
  129.   LineNr := FindLine('<img alt="cover" align="left" src="http://ia.imdb.com/media/imdb/', Page, 0);
  130.   if LineNr < 0 then
  131.     LineNr := FindLine('<img alt="cover" align="left" src="http://posters.imdb.com/', Page, 0);
  132.   if LineNr < 0 then
  133.     LineNr := FindLine('<img alt="cover" align="left" src="http://images.amazon.com/', Page, 0);
  134.   if LineNr > -1 then
  135.   begin
  136.     Line := Page.GetString(LineNr);
  137.     BeginPos := pos('src="', Line) + 4;
  138.     Delete(Line, 1, BeginPos);
  139.     EndPos := pos('"', Line);
  140.     Value := copy(Line, 1, EndPos - 1);
  141.     GetPicture(Value, False); // False = do not store picture externally ; store it in the catalog file
  142.   end;
  143.  
  144.   // Director
  145.   LineNr := FindLine('Directed by', Page, 0);
  146.   if LineNr > -1 then
  147.   begin
  148.     FullValue := '';
  149.     Line := Page.GetString(LineNr + 1);
  150.     repeat
  151.       BeginPos := pos('">', Line) + 2;
  152.       EndPos := pos('</a>', Line);
  153.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  154.       if (Value <> '(more)') and (Value <> '') then
  155.       begin
  156.         if FullValue <> '' then
  157.           FullValue := FullValue + ', ';
  158.         FullValue := FullValue + Value;
  159.       end;
  160.       Delete(Line, 1, EndPos);
  161.     until Pos('</a>', Line) = 0;
  162.     HTMLDecode(FullValue);
  163.     SetField(fieldDirector, FullValue);
  164.   end;
  165.  
  166.   // Actors
  167.   LineNr := FindLine('ast overview', Page, 0);
  168.   if LineNr = -1 then
  169.     LineNr := FindLine('redited cast', Page, 0);
  170.   if LineNr > -1 then
  171.   begin
  172.     FullValue := '';
  173.     Line := Page.GetString(LineNr);
  174.     repeat
  175.       BeginPos := Pos('<td valign="top">', Line);
  176.       if BeginPos > 0 then
  177.       begin
  178.         Delete(Line, 1, BeginPos);
  179.         Line := copy(Line, 25, Length(Line));
  180.         BeginPos := pos('">', Line) + 2;
  181.         EndPos := pos('</a>', Line);
  182.         if EndPos = 0 then
  183.           EndPos := Pos('</td>', Line);
  184.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  185.         if (Value <> '(more)') and (Value <> '') then
  186.         begin
  187.           BeginPos := pos('.... </td><td valign="top">', Line);
  188.           if BeginPos > 0 then
  189.           begin
  190.             EndPos := pos('</td></tr>', Line);
  191.             BeginPos := BeginPos + 27;
  192.             Value2 := copy(Line, BeginPos, EndPos - BeginPos);
  193.             if Value2 <> '' then
  194.             begin
  195.               Value := Value + ' (as ' + Value2 + ')';
  196.             end;
  197.           end;
  198.           if FullValue <> '' then
  199.             FullValue := FullValue + ', ';
  200.           FullValue := FullValue + Value;
  201.         end;
  202.         EndPos := Pos('</td></tr>', Line);
  203.         Delete(Line, 1, EndPos);
  204.       end else
  205.       begin
  206.         Line := '';
  207.       end;
  208.     until Line = '';
  209.     HTMLDecode(FullValue);
  210.     SetField(fieldActors, FullValue);
  211.   end;
  212.  
  213.   //Country
  214.   LineNr := FindLine('Country:', Page, 0);
  215.   if LineNr > -1 then
  216.   begin
  217.     Line := Page.GetString(LineNr + 1);
  218.     BeginPos := pos('/">', Line) + 3;
  219.     EndPos := pos('</a>', Line);
  220.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  221.     HTMLDecode(Value);
  222.     SetField(fieldCountry, Value);
  223.   end;
  224.  
  225.   //Category
  226.   LineNr := FindLine('Genre:', Page, 0);
  227.   if LineNr > -1 then
  228.   begin
  229.     Line := Page.GetString(LineNr + 1);
  230.     BeginPos := pos('/">', Line) + 3;
  231.     EndPos := pos('</a>', Line);
  232.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  233.     HTMLDecode(Value);
  234.     SetField(fieldCategory, Value);
  235.   end;
  236.  
  237.   //Description
  238.   LineNr := FindLine('Plot Summary:', Page, 0);
  239.   if LineNr < 1 then
  240.     LineNr := FindLine('Plot Outline:', Page, 0);
  241.   if LineNr > -1 then
  242.   begin
  243.     Line := Page.GetString(LineNr);
  244.     BeginPos := pos('</b>', Line) + 5;
  245.     EndPos := pos('<a href', Line);
  246.     if EndPos < 1 then
  247.     begin
  248.       Line := Line + Page.GetString(LineNr+1);
  249.       EndPos := pos('<br><br>', Line);
  250.       if EndPos < 1 then
  251.         EndPos := Length(Line);
  252.     end;
  253.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  254.     HTMLDecode(Value);
  255.     case DescrImport of
  256.       0:
  257.         begin
  258.           PickListClear;
  259.           PickListAdd(Value);
  260.           GetDescriptions(GetField(fieldURL) + 'plotsummary');
  261.           if PickListExec('Select a description for "' + MovieName + '"', Value) then
  262.             SetField(fieldDescription, Value);
  263.         end;
  264.       1:
  265.         SetField(fieldDescription, Value);
  266.       2:
  267.         SetField(fieldDescription, GetDescriptions(MovieURL + 'plotsummary'));
  268.     end;
  269.   end;
  270.  
  271.   // Comments
  272.   LineNr := FindLine('<b>Summary:</b>', Page, 0);
  273.   if LineNr > -1 then
  274.   begin
  275.     Value := '';
  276.     repeat
  277.       LineNr := LineNr + 1;
  278.       Line := Page.GetString(LineNr);
  279.       EndPos := Pos('</blockquote>', Line);
  280.       if EndPos = 0 then
  281.         EndPos := Length(Line)
  282.       else
  283.         EndPos := EndPos - 1;
  284.       Value := Value + Copy(Line, 1, EndPos) + ' ';
  285.     until Pos('</blockquote>', Line) > 0;
  286.     HTMLDecode(Value);
  287.     Value := StringReplace(Value, '<br>', #13#10);
  288.     Value := StringReplace(Value, #13#10+' ', #13#10);
  289.     SetField(fieldComments, Value);
  290.   end;
  291.  
  292.   // Length
  293.   LineNr := FindLine('Runtime:', Page, 0);
  294.   if LineNr > -1 then
  295.   begin
  296.     Line := Page.GetString(LineNr + 1);
  297.     EndPos := pos(' min', Line);
  298.     if EndPos = 0 then
  299.       EndPos := pos('  /', Line);
  300.     if EndPos = 0 then
  301.       EndPos := Length(Line);
  302.     if Pos(':', Line) < EndPos then
  303.       BeginPos := Pos(':', Line) + 1
  304.     else
  305.       BeginPos := 1;
  306.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  307.     SetField(fieldLength, Value);
  308.   end;
  309.  
  310.   // Language
  311.   LineNr := FindLine('Language:', Page, 0);
  312.   if LineNr > -1 then
  313.   begin
  314.     Line := Page.GetString(LineNr + 1);
  315.     BeginPos := pos('/">', Line) + 3;
  316.     EndPos := pos('</a>', Line);
  317.     if EndPos = 0 then
  318.       EndPos := Length(Line);
  319.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  320.     SetField(fieldLanguages, Value);
  321.   end;
  322.  
  323.   DisplayResults;
  324. end;
  325.  
  326. function GetDescriptions(Address: string): string;
  327. var
  328.   Line, Value: string;
  329.   LineNr: Integer;
  330.   BeginPos, EndPos,Longest: Integer;
  331.   Page: TStringList;
  332. begin
  333.   Result := '';
  334.   Longest := 0;
  335.   Page := TStringList.Create;
  336.   Page.Text := GetPage(Address);
  337.   LineNr := FindLine('<p class="plotpar">', Page, 0);
  338.   while LineNr > -1 do
  339.   begin
  340.     Value := '';
  341.     repeat
  342.       Line := Page.GetString(LineNr);
  343.       BeginPos := pos('"plotpar">', Line);
  344.       if BeginPos > 0 then
  345.         BeginPos := BeginPos + 10
  346.       else
  347.         BeginPos := 1;
  348.       EndPos := pos('</p>', Line);
  349.       if EndPos < 1 then
  350.         EndPos := Length(Line) + 1;
  351.       if Value <> '' then
  352.         Value := Value + ' ';
  353.       Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  354.       LineNr := LineNr + 1;
  355.     until (pos('</p>', Line) > 0) or (LineNr = Page.Count);
  356.     HTMLDecode(Value);
  357.     PickListAdd(Value);
  358.  
  359.     if Length(Value) > Longest then
  360.     begin
  361.       Result := Value;
  362.       Longest := Length(Value);
  363.     end;
  364.  
  365.     LineNr := FindLine('<p class="plotpar">', Page, LineNr);
  366.   end;
  367.   Page.Free;
  368. end;
  369.  
  370. procedure AddMoviesTitles(Page: TStringList; Tag: string);
  371. var
  372.   Line: string;
  373.   LineNr: Integer;
  374.   MovieTitle, MovieAddress: string;
  375.   StartPos: Integer;
  376. begin
  377.   LineNr := FindLine(tag, Page, 0);
  378.   if LineNr > -1 then
  379.   begin
  380.     Line := Page.GetString(LineNr);
  381.     HTMLRemoveTags(Line);
  382.     PickTreeAdd(Trim(Line), '');
  383.     LineNr := LineNr + 5;
  384.     Line := Page.GetString(LineNr);
  385.     repeat
  386.       StartPos := pos('href="', Line) + 5;
  387.       Delete(Line, 1, StartPos);
  388.       MovieAddress := Copy(Line, 1, pos('">', Line) - 1);
  389.       StartPos := Pos('">', Line) + 2;
  390.       MovieTitle := Copy(Line, StartPos, Pos('</a>', Line) - StartPos);
  391.       HTMLDecode(Movietitle);
  392.       PickTreeAdd(MovieTitle, 'http://us.imdb.com' + MovieAddress);
  393.       LineNr := LineNr + 2;
  394.       Line := Page.GetString(LineNr);
  395.     until Pos('</table>', Line) > 0;
  396.   end;
  397. end;
  398.  
  399. begin
  400.   if CheckVersion(3,4,0) then
  401.   begin
  402.     MovieName := GetField(fieldOriginalTitle);
  403.     if MovieName = '' then
  404.       MovieName := GetField(fieldTranslatedTitle);
  405.     if Input('IMDb Import', 'Enter the title of the movie:', MovieName) then
  406.     begin
  407.       AnalyzePage('http://us.imdb.com/Tsearch?title='+UrlEncode(MovieName));
  408.     end;
  409.   end
  410.   else
  411.     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
  412. end.
  413.  
  414.